﻿--[[
   @brief    descprtion lol开黑邀请tips弹窗
   @author   rickonzhang
   @version  1.0
   @date     2016-03-21
--]]

lol_kaihei_invite_tips_control = 
{
    fname_to_fun = {};
    MAX_TIPS_WND_ITEM_COUNT = 3;

    --
    optiom_item = 1;
    TIPS_ALL_INVITE_KAIHEI = 1;
    TIPS_FRIEND_INVITE_KAIHEI = 2;
    TIPS_NONE_INVITE_KAIHEI = 3;

    init = function()
        base_utility.log("[lol_kaihei_invite_tips_control] init");        

        lol_kaihei_invite_tips_control.fname_to_fun["save_setting"]       = lol_kaihei_invite_tips_control.save_setting;
        lol_kaihei_invite_tips_control.fname_to_fun["get_setting"]        = lol_kaihei_invite_tips_control.get_setting;
        lol_kaihei_invite_tips_control.fname_to_fun["close"]              = lol_kaihei_invite_tips_control.close;
        lol_kaihei_invite_tips_control.fname_to_fun["get_room_info"]      = lol_kaihei_invite_tips_control.get_room_info;
        lol_kaihei_invite_tips_control.fname_to_fun["open_kaihei_square"] = lol_kaihei_invite_tips_control.open_kaihei_square;
        lol_kaihei_invite_tips_control.fname_to_fun["join_lol_chatroom"]      = lol_kaihei_invite_tips_control.join_lol_chatroom;
    end;

    on_js_callback = function(uilib, func_name, json_str, web_ctrl_id)
        base_utility.log("[lol_kaihei_invite_tips_control] on_js_callback, fun:"..func_name..", json_str:"..json_str, true);
        local p_fun = lol_kaihei_invite_tips_control.fname_to_fun[func_name];
        if p_fun ~= nil then
            p_fun(json_str, web_ctrl_id);
        end
    end;

    is_need_to_tips = function(friend_id)
        local need = true;
        base_utility.log("[lol_kaihei_invite_tips_control] optiom_item:"..tostring(lol_kaihei_invite_tips_control.optiom_item));
        if lol_kaihei_invite_tips_control.optiom_item == lol_kaihei_invite_tips_control.TIPS_ALL_INVITE_KAIHEI then
            need = true;
        elseif lol_kaihei_invite_tips_control.optiom_item == lol_kaihei_invite_tips_control.TIPS_FRIEND_INVITE_KAIHEI then
            if social_utility.is_my_friend(friend_id) == true then
                need = true;
            else            
                need = false;    
            end
        elseif lol_kaihei_invite_tips_control.optiom_item == lol_kaihei_invite_tips_control.TIPS_NONE_INVITE_KAIHEI then
            need = false;
        end

        return need;
    end;

    on_new_friend_message_come = function(event_id, json_str)
        local param = base_utility.json_to_table(json_str);
        local message = base_utility.json_to_table(param.message);
        local sender_tgp_id = param.friend_id;

        --收到好友开黑邀请

        if message ~= nil and message.type ~= nil and 
            message.type == social_utility.MSG_TYPE_LOL_KAIHEI_INVITE then

            --消息通道有异常，自己的消息可能会发给自己一次，这里屏蔽先解决bug
            --
            if sender_tgp_id == login_model.im_login_infomation.friend_id then
                base_utility.log("[lol_kaihei_invite_tips_control] the sender is self!!!!", true);
                return;
            end


            if lol_kaihei_invite_tips_control.is_need_to_tips(sender_tgp_id) == false then
                --no need to tips
                return;
            end           
            
            local room_info = message.options;
            ierd_tgp.tpf_ui.Require("social\\lol_kaihei_invite_tips\\model.lua");
            ierd_tgp.tpf_ui.Require("social\\lol_kaihei_invite_tips\\view.lua");

            if lol_kaihei_invite_tips_model.is_room_info_wnd_exist(room_info, sender_tgp_id) == false then

                local c_count =lol_kaihei_invite_tips_model.get_wnd_item_total_count();
                if c_count >= lol_kaihei_invite_tips_control.MAX_TIPS_WND_ITEM_COUNT then
                    base_utility.log("[lol_kaihei_invite_tips_control] tips wnd count:"..tostring(c_count), true);
                    local old_wnd_item = lol_kaihei_invite_tips_model.get_the_oldes_wnd_item();
                    if old_wnd_item ~= nil then
                        old_wnd_item:show(FALSE);
                    end
                end 

                local index = c_count + 1;
                if c_count >= lol_kaihei_invite_tips_control.MAX_TIPS_WND_ITEM_COUNT then
                    index = lol_kaihei_invite_tips_control.MAX_TIPS_WND_ITEM_COUNT;
                end

                local wnd_item = lol_kaihei_invite_tips_view:new(room_info, sender_tgp_id, index);
                lol_kaihei_invite_tips_model.insert_wnd_item(wnd_item); 
            else
                base_utility.log("[lol_kaihei_invite_tips_control] on_new_friend_message_come room is exist:"..json_str, true);
            end
        end
    end;

    adjust_tips_wnd_position = function()
        local all_wnd = lol_kaihei_invite_tips_model.get_wnd_item_set();
        if #all_wnd > 0 then
            for index, tmp_wnd_item in ipairs(all_wnd) do 
                tmp_wnd_item:re_move_to_right_position(index);
            end
        end
    end;

    close = function(json_str, web_ctrl_id)
        local wnd_item = lol_kaihei_invite_tips_model.get_wnd_item(web_ctrl_id);
        if wnd_item ~= nil then
            wnd_item:show(FALSE);
        end
    end;

    save_setting = function(json_str, web_ctrl_id)
        base_utility.log("[lol_kaihei_invite_tips_control] save_setting:"..json_str, true);        
        local param_table = base_utility.json_to_table(json_str);
        lol_kaihei_invite_tips_control.optiom_item = param_table.optiom_item;

        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        local param = {optiom_item = param_table.optiom_item};
        social_network:set_lol_kaihei_cfg(base_utility.table_to_json(param));
    end;

    get_setting = function(json_str, web_ctrl_id)
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {};
        param_table["data"]["optiom_item"] = lol_kaihei_invite_tips_control.optiom_item;
       web_utility.lua_call_js_by_id("get_setting",base_utility.table_to_json(param_table), web_ctrl_id);
    end;


    on_get_lol_kaihei_cfg_result = function(event_id, json_str)
        local param_table = base_utility.json_to_table(json_str);
        if param_table.result == 0 then
            local cfg = base_utility.json_to_table(param_table.cfg_str);
            if cfg.optiom_item ~= nil then
                lol_kaihei_invite_tips_control.optiom_item = cfg.optiom_item;
            end
        end
    end;

    on_set_lol_kaihei_cfg_result = function(event_id, json_str)
        base_utility.log("[lol_kaihei_invite_tips_control] on_set_lol_kaihei_cfg_result:"..json_str, true);
    end;

    join_lol_chatroom = function(json_str, web_ctrl_id)
        local param_tbl = base_utility.json_to_table(json_str);
        local zone_id = param_tbl["zone_id"];
        local channel_id = param_tbl["channel_id"];
        local group_id = param_tbl["group_id"];
        local passwd = param_tbl["psw"];
        local room_id = param_tbl["room_id"];
        local invitor_tgp_id = param_tbl["invitor_tgp_id"];

        local tcls_id = social_network_model.tcls_zoen_id_from_lol_zone_id(zone_id);
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social_network:join_lol_chatroom(tcls_id, channel_id, group_id, passwd, room_id, invitor_tgp_id);
    end;

    --打开开黑广场
    open_kaihei_square = function(json_str, web_ctrl_id)
        comm_center.call_service("i_service_social_network_show_lol_piazza", {});
    end;

    get_room_info = function(json_str, web_ctrl_id)
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {}
        param_table["data"]["tgp_id"] = login_model.im_login_infomation["friend_id"];

        local room_info = lol_kaihei_invite_tips_model.get_room_info(web_ctrl_id);
        if room_info ~= nil then
            param_table["data"]["room_info"] = room_info;
        else
            param_table["retCode"] = 1;
            param_table["msg"] = "get room info error";
            base_utility.log("[lol_kaihei_invite_tips_control] get room info error!!!", true); 
        end
       web_utility.lua_call_js_by_id("get_room_info",base_utility.table_to_json(param_table), web_ctrl_id);
    end;

    
    --ui事件
    on_wnd_hide = function(uilib, wnd_root_id)
        local web_ctrl_id =ui_utility.get_ctrl_id_by_path(wnd_root_id, "QBWebBrowser_1");
        local wnd_item = lol_kaihei_invite_tips_model.get_wnd_item(web_ctrl_id);
        if wnd_item ~= nil then
            lol_kaihei_invite_tips_model.remove_wnd_item(wnd_item);
            wnd_item:uninit(FALSE);
        end

        lol_kaihei_invite_tips_control.adjust_tips_wnd_position();
    end;
};

lol_kaihei_invite_tips_control.init();